import { useRouter, useRoute } from "vue-router";
import routes from "./routes";

export let menus = routes.filter((item) => {
    return item.name == "layout";
})[0].children;

function getCacheList(menus) {
    let arr = [];
    menus.forEach((e) => {
        if (e?.meta?.cache) {
            arr.push(e.name);
        }
        if (e.children && e.children.length > 0) {
            arr = arr.concat(getCacheList(e.children));
        }
    });
    return arr;
}
export let cacheList = getCacheList(menus);

export let getRoutePath = (currRoute) => {
    let router = useRouter();
    let routerList = router.getRoutes();

    let path = currRoute;
    let arr = [];
    for (;;) {
        let val = routerList.find((e) => {
            return e.path == path && e.name != "layout";
        });
        let children = routerList.find((e) => {
            let parent = null;
            if (e.children && e.children?.length) {
                parent = e.children.find((e2) => {
                    return e2.path == path;
                });
            }
            return parent;
        });

        val && arr.unshift(val);
        if (children) {
            path = children.path;
        } else {
            break;
        }
    }
    return arr;
};

// console.log({
//   menus,
//   cacheList
// },'路由数据');
